package com.learn.learnplatforms.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig
{
	@Bean
	public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
		RedisTemplate<String,Object> redisTemplate = new RedisTemplate<> ();
		redisTemplate.setConnectionFactory (factory);
		redisTemplate.setKeySerializer (new StringRedisSerializer ());
		redisTemplate.setValueSerializer (new GenericJackson2JsonRedisSerializer ());
		redisTemplate.setHashKeySerializer (new StringRedisSerializer ());
		redisTemplate.setHashValueSerializer (new Jackson2JsonRedisSerializer<Object> (Object.class));
		return redisTemplate;
	}

	@Bean
	public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate){
		RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter (redisTemplate.getConnectionFactory ());
		RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig ().serializeKeysWith (
				RedisSerializationContext.SerializationPair.fromSerializer (redisTemplate.getStringSerializer ()));
		return new RedisCacheManager (redisCacheWriter,redisCacheConfiguration);
	}
}
